//
//  MZYbrandTableViewCell.m
//  WristWatch
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYbrandTableViewCell.h"
#import "MZYbrandCollectionViewCell.h"
#import "MZYbrandModel.h"
#import "MZYHeader.h"
#import "MZYbrandinfoViewController.h"


#define WIDTh self.contentView.frame.size.width
#define HEIGHt self.contentView.frame.size.height

@interface MZYbrandTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;

@property(nonatomic, retain)UICollectionViewFlowLayout *flowLayout;
@end

@implementation MZYbrandTableViewCell

- (void)dealloc {
    [_nameLabel release];
    [_nameImageView release];
    [_engLabel release];
    [_collectionView release];
    
    [super dealloc];
}

- (void)setArr:(NSMutableArray *)arr {
    if (_arr != arr) {
        _arr = arr;
        [self.collectionView reloadData];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
//    self.nameImageView = [[UIImageView alloc] init];
//    [self.contentView addSubview:self.nameImageView];
//    [_nameImageView release];
//    
//    self.nameLabel = [[UILabel alloc] init];
//    [self.contentView addSubview:self.nameLabel];
//    [_nameLabel release];
//    
//    self.engLabel = [[UILabel alloc] init];
//    [self.contentView addSubview:self.engLabel];
//    [_engLabel release];
    
//    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(375 / 2 + 0.5, 100);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTh, (self.arr.count / 2) * 100) collectionViewLayout:self.flowLayout];
    [self.contentView addSubview:self.collectionView];
    [_flowLayout release];
    [self.collectionView release];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[MZYbrandCollectionViewCell class] forCellWithReuseIdentifier:@"reuser"];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.flowLayout.itemSize = CGSizeMake(WIDTh / 2, 100);
    
    if (self.arr.count % 2 == 0) {
        self.collectionView.frame = CGRectMake(0, 0, WIDTh, (self.arr.count / 2) * 100);
    } else {
    self.collectionView.frame = CGRectMake(0, 0, WIDTh, (self.arr.count / 2 + 1) * 100);
    }
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MZYbrandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuser" forIndexPath:indexPath];
//    NSLog(@"%@", self.arr);
//    MZYbrandModel *brand =self.arr[indexPath.section][indexPath.row];
//    NSLog(@"%@", brand);
//    NSMutableArray *modelArr = self.arr[indexPath.row];
//    NSLog(@"%@", self.arr[indexPath.row]);
    MZYbrandModel *model = self.arr[indexPath.row];
    cell.nameLabel.text = model.brand_name;
    cell.nameLabel.textAlignment = 1;
    cell.engLabel.text = model.brand_keywords;;
    cell.engLabel.textAlignment = 1;
    [cell.nameImageView sd_setImageWithURL:[NSURL URLWithString:model.brand_logo] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
//    cell.nameLabel.text = brand.brand_name;
//    cell.nameLabel.textAlignment = 1;
//    cell.engLabel.text = brand.brand_keywords;
//    cell.engLabel.textAlignment =  1;
////    [cell.nameImageView sd_setImageWithURL:[NSURL URLWithString:brand.brand_logo] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
//    cell.layer.masksToBounds = YES;
    
//    NSLog(@"%@", self.arr);
    
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MZYbrandinfoViewController *brandVC = [[MZYbrandinfoViewController alloc] init];
    
    brandVC.brand = [self.arr[indexPath.row] brand_id];
#pragma mark 第三步: 设置代理人执行的协议方法
    brandVC.hidesBottomBarWhenPushed = YES;
    [self.myDelegate takeValue:brandVC];
}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
